<template>
    <el-dialog
    :title="!dataForm.id? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="100px">
      <el-form-item label="购买的服务" prop="type">
        <el-input v-model="dataForm.type" placeholder="购买的服务"></el-input>
      </el-form-item>
      <el-form-item label="联系人" prop="contactName">
        <el-input v-model="dataForm.contactName" placeholder="联系人"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="contactMobile">
        <el-input v-model="dataForm.contactMobile" placeholder="联系电话"></el-input>
      </el-form-item>
      <el-form-item label="地址信息" prop="address">
        <el-input v-model="dataForm.address" placeholder="地址信息"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" :disabled="disabled" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        disabled: false,
        dataForm: {
          id: null,
          type: null,
          contactName: null,
          contactMobile: null,
          address: null,
          createTime: null
        },
        dataRule: {
          type: [
            {required: true, message: '购买的服务不能为空', trigger: 'blur'}
          ],
          contactName: [
            {required: true, message: '联系人不能为空', trigger: 'blur'}
          ],
          contactMobile: [
            {required: true, message: '联系电话不能为空', trigger: 'blur'}
          ],
          address: [
            {required: true, message: '地址信息不能为空', trigger: 'blur'}
          ],
          createTime: [
            {required: true, message: '创建时间不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || null;
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields();
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/serviceapply/serviceApply/detail/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.type = data.item.type;
                this.dataForm.contactName = data.item.contactName;
                this.dataForm.contactMobile = data.item.contactMobile;
                this.dataForm.address = data.item.address;
                this.dataForm.createTime = data.item.createTime;
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.disabled = true;
            this.$http({
                url: this.$http.adornUrl(`/serviceapply/serviceApply/${!this.dataForm.id ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData({
                  id: this.dataForm.id || undefined,
                  type: this.dataForm.type,
                  contactName: this.dataForm.contactName,
                  contactMobile: this.dataForm.contactMobile,
                  address: this.dataForm.address,
                  createTime: this.dataForm.createTime
                })
            }).then(({data}) => {
              this.disabled = false;
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit('refreshDataList');
                  }
                })
              } else {
                this.$message.error(data.msg);
              }
            })
          }
        })
      }
    }
  }
</script>
